package com.cn.onetrip.untility;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.provider.Settings;

import com.cn.onetrip.activity.R;

public class NetworkManager {

	public static boolean CheckNetworkState(Context context) {
		boolean flag = false;
		ConnectivityManager manager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (manager.getActiveNetworkInfo() != null) {
			flag = manager.getActiveNetworkInfo().isConnected();
		}

		return flag;
	}

	public static boolean CheckWifiState(Context context) {
		boolean flag = false;
		ConnectivityManager manager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
				.getState();
		if (wifi == State.CONNECTED || wifi == State.CONNECTING)
			flag = true;

		return flag;
	}

	public static void NetworkTips(final Activity activity) {
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setTitle(R.string.network_fail);
		builder.setMessage(R.string.network_info);
		builder.setNegativeButton(R.string.confirm,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						activity.startActivity(new Intent(
								Settings.ACTION_SETTINGS));
						// android.os.Process.killProcess(android.os.Process
						// .myPid());
						activity.finish();
					}
				});
		builder.setPositiveButton(R.string.quit, new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				// android.os.Process.killProcess(android.os.Process.myPid());
				activity.finish();
			}
		});
		builder.create();
		builder.show();
	}

	public static void LoadDataTips(final Activity activity) {
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setTitle(R.string.load_data_failed);
		builder.setNegativeButton(R.string.confirm,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						activity.finish();
					}
				});
		builder.create();
		builder.show();
	}

	public static void ChargeMessage(final Context context) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(R.string.thankfully);
		builder.setMessage(R.string.thankfully_desc);
		builder.setNegativeButton(R.string.confirm,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create();
		builder.show();
	}
}
